package larrys.pomodoro.controller.ajax;

import larrys.pomodoro.domain.service.PomodoroUserService;
import larrys.pomodoro.util.AssertUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class IsUserEmailExistsController {

	private static Log logger = LogFactory
			.getLog(IsUserEmailExistsController.class);
	private PomodoroUserService userService;

	@Required
	@Autowired
	public void setUserService(PomodoroUserService userService) {
		this.userService = userService;
	}

	@ResponseBody
	@RequestMapping("/isuseremailexists")
	public String isUserEmailExists(@RequestParam("email") String email) {
		AssertUtil.argumentNotNull(email, "email");
		logger.debug("Tring to find out if user[email:" + email
				+ "] already exists");

		String result = "{\"result\":" + userService.isUserEmailExist(email) + "}";

		logger.debug("Returning:" + result);
		return result;
	}

}
